﻿using System;

/// <summary>
/// 时间转换工具
/// </summary>
public static class TimeTool
{
    /// <summary>
    /// 正常的切割字符串只能是单个字符
    /// </summary>
    public static string TimeSecondToTimeFormat(int timeCount)
    {
        string timeFormat = "";
        int second = 0;
        int minute = 0;
        int time = 0;
        /* 先算时 */
        time = timeCount / 3600;
        if (time > 0)
        {
            timeCount -= time * 3600;
        }
        /* 在算分 */
        if (timeCount > 0)
        {
            minute = timeCount / 60;
            if (minute > 0)
            {
                timeCount -= minute * 60;
            }
        }
        /* 最后算秒 */
        if (timeCount > 0)
        {
            second = timeCount;
        }
        /* 整合时分秒 */
        string timeS = time.ToString("00");
        string minuteS = minute.ToString("00");
        string secondS = second.ToString("00");
        if (time > 0)
        {
            timeFormat = timeS + ":" + minuteS + ":" + secondS;
        }
        else
        {
            timeFormat = minuteS + ":" + secondS;
        }
        return timeFormat;
    }

    /// <summary>
    /// 把秒数转换为分秒
    /// </summary>
    public static string TimeSecondToTimeMinSec(int inputTime)
    {
        string temp;
        int minute, seconds;
        minute = (int)(inputTime / 60);
        seconds = inputTime % 60;
        string minTemp = (minute < 10) ? "0" + minute : minute.ToString();
        string secTemp = (seconds < 10) ? "0" + seconds : seconds.ToString();
        temp = minTemp + ":" + secTemp;
        return temp;
    }

    /// <summary>
    /// 时间戳转换为时间字符串
    /// </summary>
    /// <param name="timeStamp">时间戳</param>
    /// <param name="formatString">时间格式串
    /// 常见的有 yyyy/MM/dd HH:mm:ss:ffff
    ///          yyyy/MM/dd
    ///          HH:mm:ss
    /// </param>
    public static string TimeStampToTimeString(double timeStamp, string formatString)
    {
        //开始时间
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
        DateTime dt = startTime.AddSeconds(timeStamp);
        return dt.ToString(formatString);
    }

    /// <summary>
    /// 获取当前时间戳
    /// </summary>
    /// <param name="needMilliSeconds">是否获取毫秒时间戳</param>
    /// <returns></returns>
    public static long GetCurTimeStamp(bool needMilliSeconds = false)
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        long ret;
        if (needMilliSeconds)
        {
            ret = Convert.ToInt64(ts.TotalMilliseconds);
        }
        else
        {
            ret = Convert.ToInt64(ts.TotalSeconds);
        }
        return ret;
    }

    /// <summary>
    /// 获取当前时间字符串
    /// </summary>
    /// <returns></returns>
    public static string GetCurTime_Y_M_D_H_M_S()
    {
        return DateTime.Now.ToString("yyyy-MM-dd  HH:mm:ss");
    }

    /// <summary>
    /// 获取当前时间字符串
    /// </summary>
    /// <returns></returns>
    public static string GetCurTimeTo_Y_M_D()
    {
        return DateTime.Now.ToString("yyyy-MM-dd");
    }

    /// <summary>
    /// 获取当前时间字符串
    /// </summary>
    /// <returns></returns>
    public static string GetCurTimeTo_M_D()
    {
        return DateTime.Now.ToString("MM-dd");
    }

    /// <summary>
    /// 获取当前时间字符串
    /// </summary>
    /// <returns></returns>
    public static string GetCurTime_H_M_S()
    {
        return DateTime.Now.ToString("HH:mm:ss");
    }

    /// <summary>
    /// 获取当前时间字符串
    /// </summary>
    /// <returns></returns>
    public static string GetCurTime_H_M()
    {
        return DateTime.Now.ToString("HH:mm");
    }

    /// <summary>
    /// 获取当前时间字符串
    /// </summary>
    /// <returns></returns>
    public static string GetCurTime_Y_M_D_H()
    {
        return DateTime.Now.ToString("yyyy-MM-dd  HH");
    }

    /// <summary>
    /// 获取当前时间字符串
    /// </summary>
    /// <returns></returns>
    public static string GetCurTime_Y_M_D_H_M()
    {
        return DateTime.Now.ToString("yyyy-MM-dd  HH:mm");
    }
}